package com.onlinevideo.service.impl;


import com.onlinevideo.dao.UserDao;
import com.onlinevideo.dao.impl.UserDaoImpl;
import com.onlinevideo.data.MyConst;
import com.onlinevideo.entity.Page;
import com.onlinevideo.entity.User;
import com.onlinevideo.service.UserService;
import com.onlinevideo.util.PageUtils;
import org.apache.commons.fileupload.FileItem;

import java.io.File;
import java.util.List;


public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public int insert(User user) {

        //插入用户数据
        user.setRoleId(4L);
        user.setEmail(user.getUsername());
        user.setUserStatus(0);
        user.setUserValidity(1);
        user.setLogoUrl("../uploadFile/images/default.jpg");
        user.setCreateTime(String.valueOf(System.currentTimeMillis()));
        return userDao.insert(user);
    }
    @Override
    public int insertBg(User user) {
        return userDao.insert(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Long id) {
        return userDao.delete(id);
    }

    @Override
    public User queryById(Long id) {
        return userDao.queryById(id);
    }

    @Override
    public List<User> queryAll() {
        return userDao.queryAll();
    }

    @Override
    public User queryByUsernameAndPassword(User user) {
        return userDao.queryByUsernameAndPassword(user);
    }

    @Override
    public Long queryByUsername(String username) {
        return userDao.queryByUsername(username);
    }

    @Override
    public List<User> queryDynamic(User user) {
        return userDao.queryDynamic(user);
    }

    @Override
    public List<User> queryLike(String username, String phone) {
        return userDao.queryLike(username, phone);
    }

    @Override
    public Long queryByMaxId() {
        return userDao.queryByMaxId();
    }

    @Override
    public Page<User> queryByPage(Integer pageSize, Integer pageNo) {
        return PageUtils.getPage(pageSize, pageNo, userDao);
    }


    @Override
    public Integer queryForPageTotalCount() {
        return userDao.queryForPageTotalCount();
    }

    @Override
    public String uploadLogo(List<FileItem> fileDomain) throws Exception {
        for (FileItem fileItem : fileDomain) {
            String contentType = fileItem.getContentType();
            if ("uploadLogo".equals(fileItem.getFieldName()) && contentType.startsWith("image")) {
                String name = fileItem.getName();
                String folder = contentType.substring(0, contentType.lastIndexOf("/"));
                String url = MyConst.FILE_UPLOAD_PATH + folder + "s/" + System.currentTimeMillis() + "_" + name;
                File file = new File("D:" +url);
                fileItem.write(file);
                return url;
            }
        }
        return null;
    }

}
